<template>
  <div class="list-container">
    <!-- 城市列表容器，设置最大高度和滚动 -->
    <div class="city-list-scroll">
      <div class="list-comp" v-for="item in weatherStore.cityList" :key="item.adcode">
        <div
          class="add-city"
          @mouseenter="handleMouseEnter(item.adcode)"
          @mouseleave="handleMouseLeave()">
          <div class="left">
            <h3>{{ item.city }}</h3>
            <span>{{ item.weather.temperature }}度</span>
          </div>
          <div class="right">
            <button @click="goSearch(item.adcode, item.city)">查看</button>
            <button @click="deleteCity(item.adcode)">删除</button>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { useWeatherStore } from '@/stores/WeatherStore'
import { useRouter } from 'vue-router'
const router = useRouter()
const weatherStore = useWeatherStore()
const hoverIndex = ref(null)
const handleMouseEnter = (adcode) => {
  hoverIndex.value = adcode
}
const handleMouseLeave = () => {
  hoverIndex.value = null
}
const goSearch = (adcode, city) => {
  router.push(`/search/${adcode}?search=${city}`)
}
const deleteCity = (adcode) => {
  weatherStore.deleteCity(adcode)
}
</script>

<style scoped lang="scss">
.list-container {
  color: white;
  // 城市列表滚动容器
  .city-list-scroll {
    //超过五个显示滚动条
    max-height: 15rem;
    //垂直滚动
    overflow-y: auto;
  }
  .list-comp {
    margin-bottom: 0.5rem;
    .add-city {
      display: flex;
      justify-content: space-between;
      position: relative;
      height: 2.5rem;
      overflow: hidden;
      .left {
        display: flex;
        align-items: center;
        width: 100%;
        justify-content: space-between;
        padding: 0.5rem 1rem;
        background-color: #004e71;
        cursor: pointer;
        transition: width 0.3s ease;
      }
      &:hover {
        .left {
          width: 80%;
        }
      }
      .right {
        display: flex;
        gap: 0.5rem;
        position: absolute;
        right: 0;
        top: 0;
        height: 100%;
        transition: opacity 0.3s ease;
        opacity: 0;
        pointer-events: none;
        button {
          width: 5rem;
          height: 100%;
          background-color: rgb(234, 179, 8);
          color: white;
          font-size: 16px;
          text-align: center;
          border: none;
          cursor: pointer;
        }
      }
      &:hover .right {
        opacity: 1;
        pointer-events: auto;
      }
    }
  }
}
</style>
